#encoding=utf8
import urllib2
import lang
import re
import md5
import net

def main():
  prov_content = net.fetch("http://s.xnimg.cn/a5078/jspro/xn.ui.currentPlace.js")
  if not prov_content: return

  city_content = net.fetch("http://s.xnimg.cn/js/cityArray.js")
  if not city_content: return

  start = prov_content.find("XN.ui.CurHome.prototype.provList=")
  if not start:
    print "can not parse http://s.xnimg.cn/a5078/jspro/xn.ui.currentPlace.js"
    return
  
  start += len("XN.ui.CurHome.prototype.provList=")
  provs = eval(prov_content[start: prov_content.find("]", start) + 1])

  data = []
  global_id = 1
  for prov in provs:
    data.append([global_id, prov])
    global_id += 1
  
  for prov in data:
    s = "var _city_%d=" % prov[0]
    start = city_content.find(s)
    if start < 0:
      print "can not get cities:",  prov[0], prov[1]
      #print city_content
      return
  
    start += len(s)
    cities = []
    temp = eval(city_content[start: city_content.find("]", start) + 1])
    for t in temp:
      ts = t.split(":")
      if len(ts) != 2: continue
      cities.append([global_id, ts[1], ts[0]])
      global_id += 1
    prov.append(cities)
  
  #re_city_id = re.compile("([0-9]+):")
  #ids = re.findall(re_city_id, js_content)

  cities_mapping = {}
  jsout = open("school_location.lib.js", "w")
  jsout.write("var _school_location=[")
  started = False
  for prov in data:
    if started: jsout.write(",")
    else: started = True
    
    jsout.write("[%d,\"%s\",[" % (prov[0], lang.escape_unicode(prov[1]).encode("utf8").strip()))
    city_started = False
    temp = {}
    for city in prov[2]:
      if city_started: jsout.write(",")
      else: city_started = True
      jsout.write("[%s,\"%s\"]" % (city[0], lang.escape_unicode(city[1]).encode("utf8").strip()))
      

      if prov[0] > 4:
        key = city[2]
	gkey = city[0]
      else:
        key = city[2][:4]
	gkey = prov[0]

      if not cities_mapping.has_key(gkey): cities_mapping[gkey] = [key]
      else:
        if not temp.has_key(key):
	  cities_mapping[gkey].append(key)
	  temp[key] = 1

    jsout.write("]")
    if prov[0] <= 4:  jsout.write(", 1")
    jsout.write("]")
  jsout.write("];")
  jsout.close()


  for city_global_id in cities_mapping.keys():
    cities = cities_mapping[city_global_id]

    urls1 = []
    urls2 = []
    urls3 = []

    for city_id in cities:
      url1 = "http://www.renren.com/pages/school/highschool/%s.html?ver=2010322" % city_id
      url2 = "http://www.renren.com/pages/school/collegeschool/%s.html?ver=2010322" % city_id
      urls1.append(url1)
      urls1.append(url2)
      urls2.append(url2)

      url3 = "http://www.renren.com/pages/school/juniorschool/%s.html?ver=2010322" % city_id
      urls3.append(url3)

    parse_school(urls1, city_global_id, "schools/1/%d.js" % city_global_id)
    parse_school(urls2, city_global_id, "schools/2/%d.js" % city_global_id)
    parse_school(urls3, city_global_id, "schools/3/%d.js" % city_global_id)


def join_school(parsed1, parsed2):
  p1 = {}
  p2 = {}

  for [name, schools] in parsed1: p1[name] = schools
  for [name, schools] in parsed2: p2[name] = schools

  for [name, schools] in parsed1:
    if not p2.has_key(name): continue
    p = {}
    for school in schools:
      p[school] = 1
    for school in p2[name]:
      if not p.has_key(name):
        schools.append(school)
  return parsed1
  

def parse_school(urls, fid, dest):
  parsed = None
  for url in urls:
    if parsed == None: parsed = parse_school0(url)
    else: parsed = join_school(parsed, parse_school0(url))

  if not parsed or len(parsed) == 0:
    print "can not fetch:%s" % url
    fout = open(dest, "w")
    fout.write("var _schools={id:\"%s\",data:[]};" % fid)
    fout.close()
    return

  fout = open(dest, "w")
  fout.write("var _schools={id:\"%s\",data:[" % fid)

  started = False
  for [district_name, schools] in parsed:

    if started: fout.write(",")
    else: started = True
    fout.write("[\"%s\",[" % district_name)

    if len(schools) > 0:
      fout.write("\"")
      fout.write("\",\"".join(schools))
      fout.write("\"")

    fout.write("]]")

  fout.write("]};")
  fout.close()

def parse_school0(url):
  content = net.fetch(url)
  if not content:
    return []

  try:
    content = lang.escape_html(content)
  except:
    content = lang.escape_html(content.decode("gbk"))

  try:
    content = content.encode("utf8")
  except:
    content = content.encode("gbk")

  result = []

  re_district = re.compile("\.tihuan\('([^']+)'\)[^>]*>([^<]+)<")
  re_district2 = re.compile("<ul[\s]+id=\"([^\"]+)\"")
  re_school_name = re.compile(">([^<]+)</a>")
  ps = re.findall(re_district, content)

  if len(ps) == 0:
    ps = re.findall(re_district2, content)
    for i in xrange(0, len(ps)):
      ps[i] = [ps[i], ""]

  for [district_id, district_name] in ps:
    start = content.find('id="%s"' % district_id)
    if not start: continue
    end = content.find('</ul>', start)
    part = content[start: end]
    schools = re.findall(re_school_name, part)
    trimed = []
    for school in schools:
      school = school.strip()
      if school != "": trimed.append(school)

    result.append([district_name, trimed])
  return result

if __name__ == "__main__":
  #parse_school(["http://www.renren.com/pages/school/highschool/1201.html?ver=2010322"], "111", "temp.js")
  #parse_school("http://www.renren.com/pages/school/collegeschool/1101.html?ver=2010322")
  #parse_school("http://www.renren.com/pages/school/juniorschool/1101.html?ver=2010322", "111", "temp.js")
  main()
